package gorrd

/*
#cgo LDFLAGS: -lrrd_th
#include <stdlib.h>
#include <rrd.h>
*/
import "C"

import (
	"fmt"
	"reflect"
	"unsafe"
)

// utility functions
func intToError(i C.int) error {
	if int(i) != 0 {
		defer C.rrd_clear_error()
		return fmt.Errorf("error: %q", C.GoString(C.rrd_strerror(i)))
	}
	return nil
}

func castCStringArrayToPointer(ar []*C.char) **C.char {
	return (**C.char)(unsafe.Pointer(&ar[0]))
}

func toCStringArray(vals []string) (C.int, []*C.char) {
	i := len(vals)
	cc := make([]*C.char, i)
	for i, v := range vals {
		cc[i] = C.CString(v)
	}
	return C.int(i), cc
}

func attachCArray(hdr *reflect.SliceHeader, sliceLen int, ptr uintptr) {
	if sliceLen > 0 {
		hdr.Cap = int(sliceLen)
		hdr.Len = int(sliceLen)
		hdr.Data = uintptr(unsafe.Pointer(ptr))
	}
}
